package akayblog;

import com.vaadin.event.ItemClickEvent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.Tree;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

@SuppressWarnings("serial")
public class MyTree extends Tree {

	private Tree tree = new Tree("Blog arşiv");

	private HorizontalLayout parentHorizontalLayout;

	private Window windowMyTree;

	public MyTree(HorizontalLayout horizontalLayout, Window window) {
		windowMyTree = window;
		this.parentHorizontalLayout = horizontalLayout;
		buildMyTree();
		treeAddListener();
		parentHorizontalLayout.addComponent(tree);
		parentHorizontalLayout.setComponentAlignment(tree, Alignment.TOP_RIGHT);

	}

	@SuppressWarnings("deprecation")
	public void buildMyTree() {
		tree.removeAllItems();
		for (Entry entry : MainWindow.entryList) {
			switch (entry.getIssueDate().getMonth() + 1) {
			case 1:
				if (!tree.hasChildren("OCAK"))
					tree.addItem("OCAK");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "OCAK");
				break;

			case 2:
				if (!tree.hasChildren("SUBAT"))
					tree.addItem("SUBAT");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "SUBAT");
				break;

			case 3:
				if (!tree.hasChildren("MART"))
					tree.addItem("MART");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "MART");
				break;

			case 4:
				if (!tree.hasChildren("NİSAN"))
					tree.addItem("NİSAN");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "NİSAN");
				break;

			case 5:
				if (!tree.hasChildren("MAYIS"))
					tree.addItem("MAYIS");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "MAYIS");
				break;

			case 6:
				if (!tree.hasChildren("HAZİRAN"))
					tree.addItem("HAZİRAN");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "HAZİRAN");
				break;

			case 7:
				if (!tree.hasChildren("TEMMUZ"))
					tree.addItem("TEMMUZ");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "TEMMUZ");
				break;

			case 8:
				if (!tree.hasChildren("AGUSTOS"))
					tree.addItem("AGUSTOS");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "AGUSTOS");
				break;

			case 9:
				if (!tree.hasChildren("EYLUL"))
					tree.addItem("EYLUL");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "EYLUL");
				break;

			case 10:
				if (!tree.hasChildren("EKİM"))
					tree.addItem("EKİM");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "EKİM");
				break;

			case 11:
				if (!tree.hasChildren("KASIM"))
					tree.addItem("KASIM");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "KASIM");
				break;

			case 12:
				if (!tree.hasChildren("ARALIK"))
					tree.addItem("ARALIK");
				tree.addItem(entry.getTitle());
				tree.setParent(entry.getTitle(), "ARALIK");
				break;

			}

		}
	}

	public void treeAddListener() {

		tree.addListener(new ItemClickEvent.ItemClickListener() {
			public void itemClick(ItemClickEvent event) {
				// Pick only left mouse clicks
				if (event.getButton() == ItemClickEvent.BUTTON_LEFT)
					for (Entry entry : MainWindow.entryList) {
						if (entry.getTitle() == event.getItemId()) {

							final Window window = new Window();

							final RichTextArea area = new RichTextArea();
							final Button close = new Button("Kapat",
									new Button.ClickListener() {

										@Override
										public void buttonClick(ClickEvent event) {
											windowMyTree.removeWindow(window);
										}
									});

							window.setWidth("50%");
							window.setHeight("100%");
							window.setModal(true);
							window.addComponent(area);
							area.setWidth("100%");
							window.addComponent(close);
							windowMyTree.addWindow(window);
							window.setCaption(entry.getTitle());
							area.setValue(entry.getText());
							area.setReadOnly(true);

						}
					}
			}
		});

	}
}
